package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.dto.RobotEquipmentControlButtonDto;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.common.entity.Button;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.core.FileProperties;
import com.hitqz.robot.biz.mapper.ButtonMapper;
import com.hitqz.robot.biz.service.ButtonService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.*;
import com.hitqz.robot.camera.type.PtzCtrlTypeEnum;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @author xupkun
 * @date 2024/6/3
 */
@Service
@Slf4j
@AllArgsConstructor
public class ButtonServiceIml extends ServiceImpl<ButtonMapper, Button> implements ButtonService {


    private final EquipmentFactory cameraFactory;

    private final FileProperties fileProperties;

    /*******************外设控制相关变量****************************/
    //记录上次控制参数,用于停止
    private static Long ptzCtrlTimeStamp = 0L;
    private static boolean ptzCtrlFlag;
    private static RobotEquipment lastEquipment;
    private static PtzCtrlDTO lastPtzCtrlDTO;
    private static QueryParamsDto lastQuery;



    @Override
    public void init() {
        ArrayList<Button> list = new ArrayList<>();
        //机器人控制按钮
        list.add(Button.createButton("robot","加速", "post","speedUp", "{\"x\":0.1, \"action\": \"speedUp\"}", "kz_btn_9", "kz_btn_9_p", "kz_btn_9_s",false));
        list.add(Button.createButton("robot","减速","post", "speedDown", "{\"x\":-0.1, \"action\": \"speedDown\"}", "kz_btn_10", "kz_btn_10_p", "kz_btn_10_s",false));
        list.add(Button.createButton("robot","前进","post", "goForward", "{\"x\":0.2, \"action\": \"goForward\"}", "kz_btn_1", "kz_btn_1_p", "kz_btn_1_s",true));
        list.add(Button.createButton("robot","后退","post", "backForward", "{\"x\": -0.2, \"action\": \"backForward\"}", "kz_btn_2", "kz_btn_2_p", "kz_btn_2_s",true));
        list.add(Button.createButton("robot","左转","post", "left", "{\"y\": 0.2, \"action\": \"left\"}", "kz_btn_6", "kz_btn_6_p", "kz_btn_6_s",true));
        list.add(Button.createButton("robot","右转", "post","right", "{\"y\": -0.2, \"action\": \"right\"}", "kz_btn_5", "kz_btn_5_p", "kz_btn_5_s",true));
        list.add(Button.createButton("robot","左平移","post", "leftTranslate", "{\"y\": 0.2, \"action\": \"leftTranslate\"}", "kz_btn_3", "kz_btn_3_p", "kz_btn_3_s",true));
        list.add(Button.createButton("robot","右平移","post", "rightTranslate", "{\"y\": -0.2, \"action\": \"rightTranslate\"}", "kz_btn_4", "kz_btn_4_p", "kz_btn_4_s",true));
        list.add(Button.createButton("robot","一键返航","post", "goHome", "{\"action\": \"goHome\"}", "kz_btn_27", "kz_btn_27_p", "kz_btn_27_s",false));
        list.add(Button.createButton("robot","充电","post", "charge", "{\"action\": \"charge\"}", "kz_btn_8", "kz_btn_8_p", "kz_btn_8_s",false));
        list.add(Button.createButton("robot","停止充电","post", "cancelCharge", "{\"action\": \"cancelCharge\"}", "kz_btn_7", "kz_btn_7_p", "kz_btn_7_s",false));
        list.add(Button.createButton("robot","急停","post", "stop", "{\"action\": \"charge\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","顶升上升","post", "jackingUp", "{\"action\": \"jackingUp\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","顶升下降","post", "jackingDown", "{\"action\": \"jackingDown\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        //挂轨
        list.add(Button.createButton("robot","远程开关机","confirm", "powerOnOrOff", "{\"action\": \"powerOnOrOff\"}", "kz_btn_17", "kz_btn_17_p", "kz_btn_17_s",false));
        //光伏
        list.add(Button.createButton("robot","继电器1开","confirm", "gf-relay", "{\"number\": \"1\",\"enable\": \"1\"}", "kz_btn_17", "kz_btn_17_p", "kz_btn_17_s",false));
        list.add(Button.createButton("robot","继电器1关","confirm", "gf-relay", "{\"number\": \"1\",\"enable\": \"0\"}", "kz_btn_18", "kz_btn_18_p", "kz_btn_18_s",false));
        list.add(Button.createButton("robot","推杆伸","post", "gf-function", "{\"function\": \"1\",\"enable\": \"1\"}", "kz_btn_1", "kz_btn_1_p", "kz_btn_1_s",false));
        list.add(Button.createButton("robot","推杆缩","post", "gf-function", "{\"function\": \"2\",\"enable\": \"1\"}", "kz_btn_2", "kz_btn_2_p", "kz_btn_2_s",false));
        list.add(Button.createButton("robot","工控机开","confirm", "gf-powerOnOrOff", "{\"number\": \"4\",\"enable\": \"1\"}", "kz_btn_17", "kz_btn_17_p", "kz_btn_17_s",false));
        list.add(Button.createButton("robot","工控机关","confirm", "gf-powerOnOrOff", "{\"number\": \"4\",\"enable\": \"2\"}", "kz_btn_18", "kz_btn_18_p", "kz_btn_18_s",false));
        list.add(Button.createButton("robot","电机开","confirm", "gf-powerOnOrOff", "{\"number\": \"3\",\"enable\": \"1\"}", "kz_btn_17", "kz_btn_17_p", "kz_btn_17_s",false));
        list.add(Button.createButton("robot","电机关","confirm", "gf-powerOnOrOff", "{\"number\": \"3\",\"enable\": \"2\"}", "kz_btn_18", "kz_btn_18_p", "kz_btn_18_s",false));
        list.add(Button.createButton("robot","滚刷开","confirm", "gf-powerOnOrOff", "{\"number\": \"2\",\"enable\": \"1\"}", "kz_btn_17", "kz_btn_17_p", "kz_btn_17_s",false));
        list.add(Button.createButton("robot","滚刷关","confirm", "gf-powerOnOrOff", "{\"number\": \"2\",\"enable\": \"2\"}", "kz_btn_18", "kz_btn_18_p", "kz_btn_18_s",false));
        list.add(Button.createButton("robot","相机开","confirm", "gf-powerOnOrOff", "{\"number\": \"1\",\"enable\": \"1\"}", "kz_btn_17", "kz_btn_17_p", "kz_btn_17_s",false));
        list.add(Button.createButton("robot","相机关","confirm", "gf-powerOnOrOff", "{\"number\": \"1\",\"enable\": \"2\"}", "kz_btn_18", "kz_btn_18_p", "kz_btn_18_s",false));
        list.add(Button.createButton("robot","编码器清零","confirm", "gf-function", "{\"function\": \"4\",\"enable\": \"1\"}", "kz_btn_35", "kz_btn_35_p", "kz_btn_35_s",false));
        list.add(Button.createButton("robot","雨刷开","post", "gf-function", "{\"function\": \"3\",\"enable\": \"1\"}", "kz_btn_35", "kz_btn_35_p", "kz_btn_35_s",false));
        list.add(Button.createButton("robot","雨刷关","post", "gf-function", "{\"function\": \"3\",\"enable\": \"2\"}", "kz_btn_35", "kz_btn_35_p", "kz_btn_35_s",false));

        list.add(Button.createButton("robot","重定位","post", "relocation", "", "kz_btn_1", "kz_btn_1_p", "kz_btn_1_s",false));

        list.add(Button.createButton("robot","开门","post", "openDoor", "{\"cmd\": 1}", "kz_btn_29", "kz_btn_29_p", "kz_btn_29_s",false));
        list.add(Button.createButton("robot","关门","post", "closeDoor", "{\"cmd\": 0}", "kz_btn_30", "kz_btn_30_p", "kz_btn_30_s",false));


        list.add(Button.createButton("robot","重定位","post", "relocation", "{\"action\": \"relocation\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","清除任务","post", "cleanTask", "{\"action\": \"cleanTask\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","抢占控制权","post", "rodController", "{\"action\": \"rodController\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","释放控制权","post", "cancelController", "{\"action\": \"cancelController\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","清除报警","post", "clearError", "{\"action\": \"clearError\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton("robot","返航","post", "turnBack", "{\"action\": \"turnBack\"}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));

        //外设按钮
        //云台
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"向上", "post","up", "" , "kz_btn_1", "kz_btn_1_p", "kz_btn_1_s",true));
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"向下", "post","down", "", "kz_btn_2", "kz_btn_2_p", "kz_btn_2_s",true));
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"向左", "post","left", "", "kz_btn_3", "kz_btn_3_p", "kz_btn_3_s",true));
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"向右", "post","right", "", "kz_btn_4", "kz_btn_4_p", "kz_btn_4_s",true));
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"补光灯亮", "post","lightOn", "", "kz_btn_15", "kz_btn_15_p", "kz_btn_15_s",false));
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"补光灯灭", "post","lightOff", "", "kz_btn_16", "kz_btn_16_p", "kz_btn_16_s",false));
        list.add(Button.createButton(EquipmentEnum.PTZ.getDes(),"雨刮", "post","wiper", "", "kz_btn_36", "kz_btn_36_p", "kz_btn_36_s",false));
        //可见光   调用 sdk 方法的 postType 写 web-sdk
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"倍数+", "post","zoomIn", "", "kz_btn_21", "kz_btn_21_p", "kz_btn_21_s", true));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"倍数-", "post","zoomOut", "", "kz_btn_22", "kz_btn_22_p", "kz_btn_2_s", true));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"焦距+", "post","focusNear", "", "kz_btn_25", "kz_btn_25_p", "kz_btn_25_s", true));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"焦距-", "post","focusFar", "", "kz_btn_26", "kz_btn_26_p", "kz_btn_26_s", true));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"拍照", "download-picture","takePic","", "kz_btn_13", "kz_btn_13_p", "kz_btn_13_s",false));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"开始录像", "post","startRecord","", "kz_btn_14", "kz_btn_14_p", "kz_btn_14_s",false));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"结束录像", "download-video","stopRecord","", "kz_btn_14", "kz_btn_14_p", "kz_btn_14_s",false));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"开启对讲", "web-sdk","talk", "", "kz_btn_19", "kz_btn_19_p", "kz_btn_19_s", false));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"关闭对讲", "web-sdk","talkClose", "", "kz_btn_20", "kz_btn_20_p", "kz_btn_20_s", false));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"自动对焦模式", "post","autoFocusMode", "", "kz_btn_24", "kz_btn_24_p", "kz_btn_24_s", false));
        list.add(Button.createButton(EquipmentEnum.VISIBLE_LIGHT_CHANNEL.getDes(),"手动对焦模式", "post","manualFocusMode", "", "kz_btn_23", "kz_btn_23_p", "kz_btn_23_s", false));
        //红外
        list.add(Button.createButton(EquipmentEnum.INFRARED_CHANNEL.getDes(),"拍照", "download-picture","takePic","", "kz_btn_13", "kz_btn_13_p", "kz_btn_13_s",false));
        //todo usb相机
        //用于外设的嵌入式控制板（控制顶升、电机等）
        list.add(Button.createButton(EquipmentEnum.CHIP.getDes(),"顶升上升", "post","jackingUp", "", "kz_btn_1", "kz_btn_1_p", "kz_btn_1_s", true));
        list.add(Button.createButton(EquipmentEnum.CHIP.getDes(),"顶升下降", "post","jackingDown", "", "kz_btn_2", "kz_btn_2_p", "kz_btn_2_s", true));
        //todo 机械臂
        list.add(Button.createButton(EquipmentEnum.ARM.getDes(),"设置零点", "post","setZeroPoint", "", "kz_btn_27", "kz_btn_27_p", "kz_btn_27_s", false));
        list.add(Button.createButton(EquipmentEnum.ARM.getDes(),"转向零点", "post","turnZeroPoint", "", "kz_btn_21", "kz_btn_21_p", "kz_btn_21_s", false));
        list.add(Button.createButton(EquipmentEnum.ARM.getDes(),"急停恢复", "post","emergencyStopRecovery", "", "kz_btn_21", "kz_btn_21_p", "kz_btn_21_s", false));

        // 车载PLC模块-变压器取油
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"复位", "post","moduleReset","", "kz_btn_13", "kz_btn_13_p", "kz_btn_13_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"急停","post", "moduleStop", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"解除急停","post", "moduleUnstop", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"模组对位","post", "moduleMatch", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"对接退出","post", "moduleMathExit", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"开始取油","post", "moduleFetOil", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"故障复位","post", "moduleErrorReset", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"左横移","post", "LeftSidesWay", "{\"distance\": 1}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.CAR_PLC.getDes(),"右横移","post", "RightSidesWay", "{\"distance\": 1}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));

        // 取油柜PLC模块-变压器取油
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"复位", "post","plcReset","", "kz_btn_13", "kz_btn_13_p", "kz_btn_13_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"急停","post", "plcStop", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"解除急停","post", "plcUnstop", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"故障复位","post", "plcErrorReset", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"开门","post", "plcDoorCtrl", "{\"door\": 1}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"关门","post", "plcDloseDoor", "{\"door\": 2}", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"模组对位","post", "plcMatch", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"对接退出","post", "plcMathExit", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));
        list.add(Button.createButton(EquipmentEnum.OIL_PLC.getDes(),"开始取油","post", "plcFetOil", "", "kz_btn_11", "kz_btn_11_p", "kz_btn_11_s",false));


        List<Button> buttonsDb = this.list();
        HashSet<String> key = new HashSet<>();
        for (Button button : CollUtil.emptyIfNull(buttonsDb)) {
            key.add(button.getAction()+button.getName()+button.getObject());
        }
        ArrayList<Button> addList = new ArrayList<>();
        for (Button button : list) {
            if (!key.contains(button.getAction()+button.getName()+button.getObject())) {
                addList.add(button);
                log.info("新增按钮：{},{},{}",button.getObject(),button.getName(),button.getAction());
            }
        }
        if (!addList.isEmpty()){
            this.saveBatch(addList);
        }

    }

    @Override
    public void handleEquipmentButton(RobotEquipmentControlButtonDto button, HttpServletResponse response) {
        RobotEquipment robotEquipment = JSON.parseObject((String) button.getParam(), RobotEquipment.class);
        IEquipmentService service = cameraFactory.getServiceByBrand(robotEquipment.getBrand());
        if (service==null){
            return;
        }
        this.equipmentButtonSimple(robotEquipment, button, service);
        this.equipmentButtonNeedReturn(robotEquipment, button,service, response);
        this.equipmentButtonNeedStop(robotEquipment, button,service);
    }


    private void equipmentButtonSimple(RobotEquipment robotEquipment, RobotEquipmentControlButtonDto buttonDto, IEquipmentService service){
        PtzCtrlDTO ptzCtrlDTO = new PtzCtrlDTO();
        ptzCtrlDTO.setIp(robotEquipment.getIp());
        ptzCtrlDTO.setChannel(robotEquipment.getChannel());
        ptzCtrlDTO.setSpeed(0);
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(robotEquipment.getPassword());
        queryParamsDto.setUsername(robotEquipment.getUsername());
        queryParamsDto.setPort(robotEquipment.getPort());
        queryParamsDto.setChannel(robotEquipment.getChannel());
        queryParamsDto.setIp(robotEquipment.getIp());
        switch (buttonDto.getAction()){
            case "lightOn"->{
                ptzCtrlDTO.setFlag(0);
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.LIGHT.getByBrand(robotEquipment.getBrand()));
                //service.ptzCtrl(ptzCtrlDTO);
                service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.PTZ_MOVE, ptzCtrlDTO);
            }
            case "lightOff"->{
                ptzCtrlDTO.setFlag(1);
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.LIGHT.getByBrand(robotEquipment.getBrand()));
                //service.ptzCtrl(ptzCtrlDTO);
                service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.PTZ_MOVE, ptzCtrlDTO);
            }
            case "wiper"->{
                ptzCtrlDTO.setFlag(0);
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.WIPER.getByBrand(robotEquipment.getBrand()));
                //service.ptzCtrl(ptzCtrlDTO);
                service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.PTZ_MOVE, ptzCtrlDTO);
            }
            case "autoFocusMode"->{
                CameraParamDTO cameraParamDTO = new CameraParamDTO();
                cameraParamDTO.setByFocusMode((byte) 2);
                cameraParamDTO.setOnlyChangeMode(true);
                //service.setCameraParam(cameraParamDTO);
                service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.FOCUS_MODE, cameraParamDTO);
            }
            case "manualFocusMode"->{
                CameraParamDTO cameraParamDTO = new CameraParamDTO();
                cameraParamDTO.setByFocusMode((byte) 1);
                cameraParamDTO.setOnlyChangeMode(true);
                //service.setCameraParam(cameraParamDTO);
                service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.FOCUS_MODE, cameraParamDTO);
            }
            case "startRecord"->{
                TakePicParamsDto paramsDto = new TakePicParamsDto();
                paramsDto.setBasePath(fileProperties.getLocal().getBasePath());
                paramsDto.setFolder(File.separator + "snapshot" + File.separator + DateUtil.format(new Date(), "yyyyMMdd") + File.separator);
                service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.START_RECORD, paramsDto);
            }
            case "reset"-> service.executeCmd(queryParamsDto, false, EquipmentCmdEnum.RESET, null);
            case "stop" -> service.executeCmd(queryParamsDto , false ,EquipmentCmdEnum.STOP, null);
            case "unstop" -> service.executeCmd(queryParamsDto , false ,EquipmentCmdEnum.UN_STOP, null);
            case "match" -> service.executeCmd(queryParamsDto , false ,EquipmentCmdEnum.START_MATCH, null);
            case "fetOil" -> {
                FetchOilDto fetchOilDto = new FetchOilDto();
                fetchOilDto.setSn(0);
                service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.FET_OIL, fetchOilDto);
            }
            case "mathExit"-> service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.MATCH_EXIT, null);
            case "errorReset" -> service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.ERROR_RESET, null);
            case "sidesWay"-> {
                SidesWayDto sidesWayDto = new SidesWayDto();
                //JSONObject jsonObject = JSONObject.parseObject(String.valueOf(buttonDto.getParam()));
                sidesWayDto.setDistance(-3);
                service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.ENABLE_SIDES, sidesWayDto);
            }
            case "enableSidesWay"-> service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.ENABLE_SIDES_WAY, null);
            case "doorCtrl" -> {
                DoorCtrlDto doorCtrlDto = new DoorCtrlDto();
                doorCtrlDto.setDoor(1);
                service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.PLC_DOOR_CTRL, doorCtrlDto);
            }
            case "closeDoor" -> {
                DoorCtrlDto doorCtrlDto = new DoorCtrlDto();
                doorCtrlDto.setDoor(2);
                service.executeCmd(queryParamsDto , true ,EquipmentCmdEnum.PLC_DOOR_CTRL, doorCtrlDto);
            }
            case "jackingUp"->{
                service.executeCmd(queryParamsDto,true,EquipmentCmdEnum.JACKING_UP,null);
            }
            case "jackingDown"->{
                service.executeCmd(queryParamsDto,true,EquipmentCmdEnum.JACKING_DOWN,null);
            }

        }
    }


    private void equipmentButtonNeedStop(RobotEquipment robotEquipment, RobotEquipmentControlButtonDto buttonDto, IEquipmentService service){
        //控制
        PtzCtrlDTO ptzCtrlDTO = new PtzCtrlDTO();
        ptzCtrlDTO.setIp(robotEquipment.getIp());
        ptzCtrlDTO.setChannel(robotEquipment.getChannel());
        ptzCtrlDTO.setSpeed(4);
        ptzCtrlDTO.setFlag(0);
        boolean ctrl = false;
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(robotEquipment.getPassword());
        queryParamsDto.setUsername(robotEquipment.getUsername());
        queryParamsDto.setPort(robotEquipment.getPort());
        queryParamsDto.setChannel(robotEquipment.getChannel());
        queryParamsDto.setIp(robotEquipment.getIp());
        switch (buttonDto.getAction()){
            case "up"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.TILT_UP.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "down"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.TILT_DOWN.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "left"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.PAN_LEFT.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "right"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.PAN_RIGHT.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "zoomIn"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.ZOOM_IN.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "zoomOut"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.ZOOM_OUT.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "focusNear"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.FOCUS_NEAR.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
            case "focusFar"->{
                ptzCtrlDTO.setCtrlType(PtzCtrlTypeEnum.FOCUS_FAR.getByBrand(robotEquipment.getBrand()));
                ptzCtrlTimeStamp = System.currentTimeMillis();
                ctrl = true;
            }
        }
        if (ctrl && !ptzCtrlFlag) {
            //service.ptzCtrl(ptzCtrlDTO);
            service.executeCmd(queryParamsDto, false, EquipmentCmdEnum.PTZ_MOVE, ptzCtrlDTO);
            lastPtzCtrlDTO =  ptzCtrlDTO;
            lastEquipment = robotEquipment;
            lastQuery = queryParamsDto;
            ptzCtrlFlag = true;
        }
    }


    private void equipmentButtonNeedReturn(RobotEquipment robotEquipment, RobotEquipmentControlButtonDto buttonDto, IEquipmentService service, HttpServletResponse response ){
        //抓拍
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(robotEquipment.getPassword());
        queryParamsDto.setUsername(robotEquipment.getUsername());
        queryParamsDto.setPort(robotEquipment.getPort());
        queryParamsDto.setChannel(robotEquipment.getChannel());
        queryParamsDto.setIp(robotEquipment.getIp());
        switch (buttonDto.getAction()){
            case "takePic"->{
                //MediaFileDto mediaFileDto = service.takePic(robotEquipment.getIp(), robotEquipment.getChannel(), fileProperties.getLocal().getBasePath(), File.separator + "snapshot" + File.separator + DateUtil.format(new Date(), "yyyyMMdd") + File.separator);
                TakePicParamsDto paramsDto = new TakePicParamsDto();
                paramsDto.setBasePath(fileProperties.getLocal().getBasePath());
                paramsDto.setFolder(File.separator + "snapshot" + File.separator + DateUtil.format(new Date(), "yyyyMMdd") + File.separator);
                MediaFileDto mediaFileDto = (MediaFileDto) service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.TAKE_PIC,paramsDto);
                if (mediaFileDto!=null) {
                    String fileName = URLEncoder.encode("snapshot", StandardCharsets.UTF_8);
                    if (response==null){
                        //服务端发过来的

                        return;
                    }
                    response.setContentType("application/octet-stream");
                    response.setCharacterEncoding("utf-8");
                    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".jpeg");
                    try {
                        ServletOutputStream outputStream = response.getOutputStream();
                        outputStream.write(FileUtil.readBytes(mediaFileDto.getAbsolutePath()));
                        outputStream.close();
                        log.error("抓拍返回图片");
                    }catch (Exception e){
                        log.error("抓拍到 web 失败",e);
                    }
                }
            }
            case "stopRecord"->{
                TakePicParamsDto paramsDto = new TakePicParamsDto();
                paramsDto.setBasePath(fileProperties.getLocal().getBasePath());
                paramsDto.setFolder(File.separator + "snapshot" + File.separator + DateUtil.format(new Date(), "yyyyMMdd") + File.separator);
                MediaFileDto mediaFileDto = (MediaFileDto) service.executeCmd(queryParamsDto, true, EquipmentCmdEnum.STOP_RECORD,paramsDto);
                //service.stopRecord(robotEquipment.getIp(), robotEquipment.getChannel(),
                // fileProperties.getLocal().getBasePath(),
                // File.separator + "snapshot" + File.separator + DateUtil.format(new Date(), "yyyyMMdd") + File.separator);
                if (mediaFileDto!=null) {
                    String fileName = URLEncoder.encode("snapshot", StandardCharsets.UTF_8);
                    if (response==null){
                        //服务端发过来的

                        return;
                    }
                    response.setContentType("application/octet-stream");
                    response.setCharacterEncoding("utf-8");
                    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".mp4");
                    try {
                        ServletOutputStream outputStream = response.getOutputStream();
                        outputStream.write(FileUtil.readBytes(mediaFileDto.getAbsolutePath()));
                        outputStream.close();
                    }catch (Exception e){
                        log.info("录像到 web 失败",e);
                    }
                }
            }
        }
    }



    @Scheduled(fixedDelay = 300)
    public void checkPtzCtrl() {
        if (ptzCtrlFlag && System.currentTimeMillis()-ptzCtrlTimeStamp>=300) {
            if (lastEquipment == null || lastPtzCtrlDTO == null || lastQuery == null){
                return;
            }
            IEquipmentService service = cameraFactory.getServiceByBrand(lastEquipment.getBrand());
            if (service==null){
                return;
            }
            lastPtzCtrlDTO.setFlag(1);
            //service.ptzCtrl(lastPtzCtrlDTO);
            service.executeCmd(lastQuery, true, EquipmentCmdEnum.PTZ_MOVE, lastPtzCtrlDTO);
            log.info("stop ptz");
            ptzCtrlFlag = false;
            lastPtzCtrlDTO = null;
            lastEquipment = null;
            lastQuery = null;
        }
    }



}
